package com.core.wigets;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.core.R;
import com.core.util.DisplayUtil;

public class TitleView extends FrameLayout {

    public ImageView mLeftBtn;
    public TextView mRightBtn;
    public ImageView title_right_image;
    public ImageView title_right_iv;
    public ImageView title_close_back;
    public TextView mTitle;
    public View backView;

    private OnClickListener mOnTitleClickListener;

    private OnClickListener mOnLeftButtonClickListener;
    private OnClickListener mOnRightButtonClickListener;
    private OnClickListener mOnCloseBackClickListener;

    public IBackListener iBackListener;
    private IHomeListener iHomeListener;
    private Resources res;

    public TitleView(Context context) {
        this(context, null);
        res = context.getResources();
    }

    public TitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        res = context.getResources();
    }

    public TitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.template_title, this, true);
        mTitle = (TextView) view.findViewById(R.id.title_text);
        mLeftBtn = (ImageView) view.findViewById(R.id.title_left_btn);
        title_right_image = (ImageView) findViewById(R.id.title_right_image);
        title_right_iv = (ImageView) findViewById(R.id.title_iv_right);
        title_close_back = (ImageView) findViewById(R.id.title_close_back);

        title_right_iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightButtonClickListener != null) {
                    mOnRightButtonClickListener.onClick(v);
                }
            }
        });

        title_right_image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightButtonClickListener != null) {
                    mOnRightButtonClickListener.onClick(v);
                }
            }
        });
        mLeftBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iBackListener != null) {
                    iBackListener.backListener();
                } else if (mOnLeftButtonClickListener != null) {
                    mOnLeftButtonClickListener.onClick(v);
                }
            }
        });
        mRightBtn = (TextView) view.findViewById(R.id.title_right_text);
        mRightBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightButtonClickListener != null) {
                    mOnRightButtonClickListener.onClick(v);
                }
            }
        });

        mTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnTitleClickListener != null) {
                    mOnTitleClickListener.onClick(v);
                }
            }
        });

        title_close_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCloseBackClickListener != null) {
                    mOnCloseBackClickListener.onClick(v);
                }
            }
        });
        backView = view.findViewById(R.id.backView);
    }

    public void setTitleName(String text) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(text);
    }

    public String getTitleName() {
        return mTitle.getText().toString();
    }

    public void setTitleName(String text, OnClickListener listener) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(text);
        this.mOnTitleClickListener = listener;
    }

    public void setTitleName(int stringID) {
        setTitleName(res.getString(stringID));
    }

    public void setTitleLeftImageButton(int imgID, OnClickListener listener) {
        mLeftBtn.setImageResource(imgID);
        mLeftBtn.setVisibility(View.VISIBLE);
        mOnLeftButtonClickListener = listener;
    }

    public void setTitleLeftButtonListener(OnClickListener listener) {
        mOnLeftButtonClickListener = listener;
    }

    public void setOnCloseBackClickListener(OnClickListener onCloseBackClickListener) {
        title_close_back.setVisibility(VISIBLE);
        mOnCloseBackClickListener = onCloseBackClickListener;
    }

    /**
     * 设置右按钮文本和事件
     */
    public void setTitleRightButtonText(String text, OnClickListener listener) {
        mRightBtn.setVisibility(View.VISIBLE);
        mRightBtn.setText(text);
        mRightBtn.setOnClickListener(listener);
        mRightBtn.setCompoundDrawables(null, null, null, null);
    }

    /**
     * 设置右按钮文本和事件
     */
    public void setTitleRightButtonText(String text, @ColorInt int color, OnClickListener listener) {
        mRightBtn.setVisibility(View.VISIBLE);
        mRightBtn.setText(text);
        mRightBtn.setTextColor(color);
        mRightBtn.setOnClickListener(listener);
        mRightBtn.setCompoundDrawables(null, null, null, null);
    }



    /**
     * 设置右按钮箭头
     * true为向下,false为向上
     */
    public void setTitleRightButtonArrow(String text, Boolean upOrDowm) {
        mRightBtn.setText(text);
        setTitleRightButtonArrow(upOrDowm);
    }

    public void setTitleRightButtonArrow(Boolean upOrDowm) {
        mRightBtn.setVisibility(View.VISIBLE);
        Drawable drawable = null;
        if (upOrDowm) {
            drawable = getResources().getDrawable(R.drawable.errow_drop_up_white);
        } else {
            drawable = getResources().getDrawable(R.drawable.errow_drop_down_white);
        }
        drawable.setBounds(0, 0, DisplayUtil.dip2px(getContext(), 16), DisplayUtil.dip2px(getContext(), 8));
        mRightBtn.setCompoundDrawables(null, null, drawable, null);
        mRightBtn.setCompoundDrawablePadding(DisplayUtil.dip2px(getContext(), 3));
    }

    /**
     * 设置右按钮文本和事件
     */
    public void setTitleRightButtonTextAndErrow(String text, OnClickListener listener) {
        mRightBtn.setVisibility(View.VISIBLE);
        mRightBtn.setText(text);
        mRightBtn.setOnClickListener(listener);
        mRightBtn.setMinWidth(DisplayUtil.dip2px(getContext(), 65));
        Drawable drawable = getResources().getDrawable(R.drawable.errow_drop_down_white);
        drawable.setBounds(0, 0, 28, 15);
        mRightBtn.setCompoundDrawables(null, null, null, drawable);
    }


    /**
     * 设置右按钮文本和事件
     */
    public void setTitleRightButtonTextAndArrow(String text, OnClickListener listener) {
        mRightBtn.setVisibility(View.VISIBLE);
        mRightBtn.setText(text);
        mRightBtn.setOnClickListener(listener);
        Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.errow_drop_down_white);
        drawable.setBounds(0, 0, DisplayUtil.dip2px(getContext(), 16), DisplayUtil.dip2px(getContext(), 8));
        mRightBtn.setCompoundDrawablePadding(DisplayUtil.dip2px(getContext(), 3));
        mRightBtn.setCompoundDrawables(null, null, drawable, null);
    }

    public void updateRightButtonText(String text) {
        mRightBtn.setText(text);
    }


    /**
     * 设置右按钮背景和事件
     */
    public void setTitleRightImageButton(int imageResId, OnClickListener listener) {
        title_right_image.setBackgroundResource(imageResId);
        title_right_image.setVisibility(View.VISIBLE);
        mOnRightButtonClickListener = listener;
    }

    public void setTitleRightImageButton(int imageResId) {
        title_right_image.setBackgroundResource(imageResId);
    }

    public void setTitleRightImage(int imageResId) {
        title_right_iv.setVisibility(VISIBLE);
        title_right_iv.setImageResource(imageResId);
        title_right_image.setVisibility(GONE);
    }

    public void updateTitleRightImage(int imageResId) {
        title_right_iv.setImageResource(imageResId);
    }

    public void setTitleRightImage(int imageResId, OnClickListener listener) {
        setTitleRightImage(imageResId);
        mOnRightButtonClickListener = listener;
    }


    public void setRightImageVisble(boolean isShow) {
        if (isShow) {
            title_right_image.setVisibility(View.VISIBLE);
        } else {
            title_right_image.setVisibility(View.GONE);
        }
    }

    public void setTitleBarColor(int color) {
        backView.setBackgroundColor(getResources().getColor(color));
        mRightBtn.setTextColor(getResources().getColor(R.color.white));
        mTitle.setTextColor(getResources().getColor(R.color.white));
    }


    public void hiddenTitleLeftButton() {
        mLeftBtn.setVisibility(View.INVISIBLE);
    }

    public void hiddenTitleRightButton() {
        mRightBtn.setVisibility(View.INVISIBLE);
    }

    public void showTitleRightButton() {
        mRightBtn.setVisibility(View.VISIBLE);
    }

    public interface IBackListener {
        void backListener();
    }

    public interface IHomeListener {
        void goHomeListener();
    }
}
